from django.db import models
import time
from com.kjssoft.common.globals import DBTableAssist
from com.kjssoft.eagleye.models import EEUser
from com.kjssoft.rainbow.category.models import RainbowPhotoFrameCategory

__author__ = 'Sea'

class RainbowPhotoFrameAlbumManager(models.Manager):
    def get_default_album(self, user):
        try:
            album = self.get(
                name = 'default',
                user = user
            )
        except RainbowPhotoFrameAlbum.DoesNotExist as e:
            album = self.create(
                name = 'default',
                user = user,
                create_ms = int(time.time()),
                status = 1
            )
            album.categories.add(RainbowPhotoFrameCategory.objects.get(name = 'default'))
        return album

class RainbowPhotoFrameAlbum(models.Model):
    user        = models.ForeignKey(EEUser, related_name='photo_frame_user')
    create_ms   = models.IntegerField()
    status      = models.IntegerField()
    name        = models.CharField(max_length=128)
    nickname    = models.CharField(max_length=128, null=True, blank=True)
    categories  = models.ManyToManyField(RainbowPhotoFrameCategory, related_name='photo_frame_album_categories')
    description = models.CharField(max_length=500, null=True, blank=True)

    objects = RainbowPhotoFrameAlbumManager()

    def __unicode__(self):
        return 'rainbow photo frame album %s' % self.name

    class Meta:
        db_table = DBTableAssist.get_db_table_name('rainbow_photo_frame_album')
        unique_together = (
            ('user', 'name'),
        )
